package com.onlineauction.entities;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.onlineauction.helpers.Constants;

public class Auction extends GenericEntity {

	private long itemID;
	private double askPrice;
	private Calendar startTime = null;
	private Calendar endTime = null;
	private boolean isActive = false;
	private long auctionID = -1;

	public Auction(long itemID, double askPrice, Calendar startTime, Calendar endTime, boolean isActive,
			boolean toCreate) {
		this.itemID = itemID;
		this.askPrice = askPrice;
		this.startTime = startTime;
		this.endTime = endTime;
		this.isActive = isActive;

		if (toCreate) {
			this.createAndStoreAuction();
		}
	}
	
	public Auction(long auctionID) {
		Entity entity = getEntity(Constants.AUCTION, auctionID);
		if (entity != null) {
			this.auctionID = entity.getKey().getId();
			this.itemID = (Long) entity.getProperty(Constants.ITEM_ID);
			this.askPrice = (Double) entity.getProperty(Constants.ASK_PRICE);
			this.startTime = (Calendar) entity.getProperty(Constants.START_TIME);
			this.endTime = (Calendar) entity.getProperty(Constants.END_TIME);
			this.isActive = (Boolean) entity.getProperty(Constants.IS_ACTIVE);			
		}				
	}

	private void createAndStoreAuction() {
		Entity entity = new Entity(Constants.AUCTION);
		entity.setProperty(Constants.ITEM_ID, this.itemID);
		entity.setProperty(Constants.ASK_PRICE, this.askPrice);
		entity.setProperty(Constants.START_TIME, this.startTime);
		entity.setProperty(Constants.END_TIME, this.endTime);
		entity.setProperty(Constants.IS_ACTIVE, this.isActive);

		Key key = storeEntity(entity);
		if (key != null) {
			this.auctionID = key.getId();
		}
	}

	public static List<Auction> getAuctionsOfItem(Long itemID) {
		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		@SuppressWarnings("deprecation")
		Query q = new Query(Constants.AUCTION).addFilter(Constants.ITEM_ID, FilterOperator.EQUAL,
				itemID);

		Iterable<Entity> entities = ds.prepare(q).asIterable();
		List<Auction> auctions = new ArrayList<Auction>();
		for (Entity entity : entities) {
			Auction auction = new Auction((Long) entity.getProperty(Constants.ITEM_ID),
					(Double) entity.getProperty(Constants.ASK_PRICE),
					(Calendar) entity.getProperty(Constants.START_TIME),
					(Calendar) entity.getProperty(Constants.END_TIME),
					(Boolean) entity.getProperty(Constants.IS_ACTIVE), false);
			auction.auctionID = entity.getKey().getId();
			auctions.add(auction);
		}
		return auctions;
	}
	
	public void cancelAuction() {
		if (this.auctionID != -1) {
			Entity entity = getEntity(Constants.AUCTION, this.auctionID);
			if (entity != null) {
				entity.setProperty(Constants.IS_ACTIVE, false);
				storeEntity(entity);
			}
		}
	}

	public long getItemID() {
		return itemID;
	}

	public double getAskPrice() {
		return askPrice;
	}

	public Calendar getStartTime() {
		return startTime;
	}

	public Calendar getEndTime() {
		return endTime;
	}

	public boolean isActive() {
		return isActive;
	}

	public long getAuctionID() {
		return auctionID;
	}
	
			
}
